import type { Ihandler, IMExtendedDocument } from "@/types/dome";
export const checkFull = () => {
  const htmlDocument: IMExtendedDocument = document;
  const isFull =
    htmlDocument.mozFullScreen ||
    htmlDocument.fullScreen ||
    htmlDocument.webkitIsFullScreen ||
    htmlDocument.webkitRequestFullScreen ||
    htmlDocument.mozRequestFullScreen ||
    htmlDocument.msFullscreenEnabled;
  return !!isFull;
};

export class WindowResizeChange {
  private resizeThrottled: boolean = false;
  private delay: number;
  public callback: any = () => {};
  constructor(handler: Ihandler, delay = 250) {
    this.delay = delay;
    // 写法1
    /*const { handleResize } = this;
    const functionName = handleResize.bind(this, handler);
    functionName();*/
    // 写法2
    this.handleResize(handler);
  }
  checkIsMobile() {
    const clientWidth = window.innerWidth;
    return clientWidth <= 750;
  }
  resizeListener(handler: Ihandler) {
    let { delay, checkIsMobile } = this;
    if (!this.resizeThrottled) {
      this.resizeThrottled = true;
      const timer = setTimeout(() => {
        handler(checkIsMobile());
        this.resizeThrottled = false;
        clearTimeout(timer);
      }, delay);
    }
  }
  handleResize(handler: Ihandler) {
    const { resizeListener } = this;
    this.callback = resizeListener.bind(this, handler);
    window.addEventListener("resize", this.callback, false);
  }
}

export function formatFileSize(size: number, pointLength = 2) {
  if (size < 1024) {
    return size + "B";
  } else if (size < 1024 * 1024) {
    return (size / 1024).toFixed(pointLength) + "KB";
  } else {
    return (size / 1024 / 1024).toFixed(pointLength) + "MB";
  }
}

export const getFileLocalUrl = (file: File): Promise<any> => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e: any) {
      resolve(e.target.result);
    };
    reader.onerror = function (e: any) {
      reject(e);
    };
  });
};
export const getFileName = (url: string) => {
  const arr = url.split("/");
  return arr[arr.length - 1];
};
